package tests;


import org.junit.Before;
import org.junit.Test;

import tools.Tester;
import static org.junit.Assert.*;

public class ProductStatsReportTest
{
	
	private final String path = "test-src-drewski/files/";
	
	@Before
	public void setUp()
	{
		Tester.END_DAY = 2;
		Tester.END_MONTH = 4;
		Tester.END_YEAR = 2012;
		Tester.REPORT_LEN = 1;
	}
	/**
	 * Each test below imports the described xml, runs the report, and checks against 
	 * html output files from the demo
	 */
	
	@Test
	public void Test1()
	{
		String root = "test-all-items-added-and-removed-before-period";		
		runTest(root);
	}
	
	@Test
	public void Test2()
	{
		String root = "test-all-items-added-before-period";		
		runTest(root);
	}
	
	@Test
	public void Test3()
	{
		String root = "test-all-items-added-on-first-day-of-period";		
		runTest(root);
	}
	
	@Test
	public void Test4()
	{
		String root = "test-all-items-added-on-last-day-of-period";		
		runTest(root);
	}
	
	@Test
	public void Test5()
	{
		String root = "test-items-added-removed-on-each-period-day";		
		runTest(root);
	}
	
	@Test
	public void Test6()
	{
		String root = "test-no-items-added";		
		runTest(root);
	}
	
	@Test
	public void Test7()
	{
		// These tests are designed for a 1 month period
		// Changing the report period length to 3 months 
		// should cause a fail
		Tester.REPORT_LEN = 3; 
		
		String root = "test-all-items-added-and-removed-before-period";		
		runFalseTest(root);
		System.out.println("The above test was supposed to fail.");
	}
	
	@Test
	public void Test8()
	{
		// These tests are designed for a 1 month period
		// Changing the report period length to -5 months 
		// should cause a fail and an exception error to
		// be thrown
		Tester.REPORT_LEN = -5; 
		
		String root = "test-all-items-added-and-removed-before-period";		
		runFalseTest(root);
		System.out.println("The above test was supposed to fail.");
	}
	
	
	
	
	private void runTest(String root)
	{
		String xml = root + ".xml";
		String html = root + ".html";
		
		Tester results = new Tester(path + xml, path + html);
		
		if ( !results.success() )
		{
			System.out.println("\n" + root.toUpperCase());
			results.displayErrors();
		}
		else
			System.out.println("\n" + root.toUpperCase() + "\nSUCCESS!!\n");
		
		org.junit.Assert.assertTrue( results.success() );
		
		
	}
	
	private void runFalseTest(String root)
	{
		String xml = root + ".xml";
		String html = root + ".html";
		
		Tester results = new Tester(path + xml, path + html);
		
		if ( !results.success() )
		{
			System.out.println("\n" + root.toUpperCase());
			results.displayErrors();
		}
		else
			System.out.println("\n" + root.toUpperCase() + "\nSUCCESS!!\n");
		
		org.junit.Assert.assertTrue( !results.success() );
		
		
	}
	
}
